package com.ulife.permission.manager.impl;

import java.sql.SQLException;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.ulife.common.Result;
import com.ulife.common.exception.ManagerException;
import com.ulife.permission.common.DepartmentQueryResult;
import com.ulife.permission.dao.DepartmentDao;
import com.ulife.permission.dao.dataobject.DepartmentDO;
import com.ulife.permission.dao.dataobject.RoleDO;
import com.ulife.permission.manager.DepartmentManager;

public class DepartmentManagerImpl implements DepartmentManager {
	
	@Autowired
	private DepartmentDao departmentDao;

	public DepartmentQueryResult getAllDepartment() throws ManagerException {
		DepartmentQueryResult result = new DepartmentQueryResult();
		List<DepartmentDO> departmentList;
		try {
			departmentList = departmentDao.getAllDepartment();
			if (CollectionUtils.isNotEmpty(departmentList)) {
				result.setDepartmentList(departmentList);
			}
			result.setSuccess(true);
			return result;
		} catch (SQLException e) {
			throw new ManagerException(e);
		}
	}

	public Result<DepartmentDO> addDepartment(DepartmentDO department) throws ManagerException {
		Result<DepartmentDO> result = new Result<DepartmentDO>();
		try {
			int id = departmentDao.addDepartment(department);
			if (id > 0) {
				result.setSuccess(true);
			}
			return result;
		} catch (SQLException e) {
			throw new ManagerException(e);
		}
	}

	public Result<Integer> delDepartment(long departmentId) throws ManagerException {
		Result<Integer> result = new Result<Integer>();
		try {
			int i = departmentDao.delDepartment(departmentId);
			if (i > 0) {
				result.setSuccess(true);
			}
			return result;
		} catch (SQLException e) {
			throw new ManagerException(e);
		}
	}

	public Result<Integer> updateDepartment(DepartmentDO department) throws ManagerException {
		Result<Integer> result = new Result<Integer>();
		try {
			int i = departmentDao.updateDepartment(department);
			if (i > 0) {
				result.setSuccess(true);
			}
			return result;
		} catch (SQLException e) {
			throw new ManagerException(e);
		}
	}

	public Result<DepartmentDO> getDepartmentById(long departmentId)
			throws ManagerException {
		Result<DepartmentDO> result = new Result<DepartmentDO>();
		try {
			DepartmentDO department = departmentDao.getDepartmentById(departmentId);
			if (department != null) {
				result.setSuccess(true);
				result.setModule(department);
			}
			return result;
		}catch (SQLException e) {
			throw new ManagerException(e);
		}
	}

	public DepartmentQueryResult getAllAvailableDepartment() throws ManagerException {
		DepartmentQueryResult result = new DepartmentQueryResult();
		List<DepartmentDO> departmentList;
		try {
			departmentList = departmentDao.getAllAvailableDepartment();
			if (CollectionUtils.isNotEmpty(departmentList)) {
				result.setDepartmentList(departmentList);
			}
			result.setSuccess(true);
			return result;
		} catch (SQLException e) {
			throw new ManagerException(e);
		}
	}

}
